var Table = function () {
    function Sorter(n) {
        this.n = n;
        this.table;
        this.tableBody;
        this.tableRows;
        this.d;
        this.p;
        this.columnCount;
        this.dataArray = [];
        this.l = 0
    }

    Sorter.prototype.init = function (table, column) {
        this.table = document.getElementById(table);
        this.tableBody = this.table.getElementsByTagName('tbody')[0];
        this.tableRows = this.tableBody.rows;

        var rowCount = this.tableRows.length;

        for (var i = 0; i < rowCount; i++) {
            if (i == 0) {
                var columns = this.tableRows[i].cells;
                this.columnCount = columns.length;

                for (var x = 0; x < this.columnCount; x++) {
                    if (columns[x].className != 'nosort') {
                        columns[x].className = 'head';
                        columns[x].onclick = new Function(this.n + '.work(this.cellIndex)')
                    }
                }
            } else {
                this.dataArray[i - 1] = {};
                this.l++;
            }
        }

        if (column != null) {
            var a = new Function(this.n + '.work(' + column + ')');
            a()
        }
    }

    Sorter.prototype.work = function (column) {
        this.tableBody = this.table.getElementsByTagName('tbody')[0];
        this.tableRows = this.tableBody.rows;
        var x = this.tableRows[0].cells[column], i;

        for (i = 0; i < this.l; i++) {
            this.dataArray[i].o = i + 1;
            var v = this.tableRows[i + 1].cells[column].firstChild;
            this.dataArray[i].value = (v != null) ? v.nodeValue : ''
        }

        for (i = 0; i < this.columnCount; i++) {
            var c = this.tableRows[0].cells[i];
            if (c.className != 'nosort') {
                c.className = 'head'
            }
        }

        if (this.p == column) {
            this.dataArray.reverse();
            x.className = (this.d) ? 'asc' : 'desc';
            this.d = (this.d) ? false : true
        } else {
            this.d = false
            this.p = column;
            this.dataArray.sort(compare);
            x.className = 'asc';
        }

        var n = document.createElement('tbody');
        n.appendChild(this.tableRows[0]);
        for (i = 0; i < this.l; i++) {
            var r = this.tableRows[this.dataArray[i].o - 1].cloneNode(true);
            n.appendChild(r);
            r.className = (i % 2 == 0) ? 'even' : 'odd'
        }

        this.table.replaceChild(n, this.tableBody)
    }

    function compare(f, c) {
        f = f.value;
        c = c.value;

        var i = parseFloat(f.replace(/(\$|\,)/g, ''));
        var n = parseFloat(c.replace(/(\$|\,)/g, ''));

        if (!isNaN(i) && !isNaN(n)) {
            f = i;
            c = n
        }

        return (f > c ? 1 : (f < c ? -1 : 0))
    }

    return {
        sorter:Sorter
    }
}();